app.controller("class-subjectController",function($scope, $http,$routeParams){
             $scope.class_id=$routeParams.id;
             $scope.class_name=$routeParams.name;
             console.log($scope.class_id)
             $http({
                 method: 'POST',
                 url: '/OA/classsubject/list',
                 data:$.param({class_id:$scope.class_id}),
                 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
            }).success(function (response) {
                 console.log(response)               //总条数
                 $scope.total = response.data.length;
                 //反转函数转化abcd转dcba
                 $scope.data = response.data;
                 //选择显示的条数
                 $scope.values = [{"limit":6}];
                 //默认显示的条数
                 $scope.selectedLimit=$scope.values[0];
                 //默认显示当前页数
                 $scope.currentPage = 1;
                 if($scope.data != null){
                     $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.currentPage-1),$scope.selectedLimit.limit*$scope.currentPage);
                 }else{
                     alert($scope.data);
                 }
                 $scope.page = {
                     "limit":$scope.selectedLimit.limit,"pageSize":5,"pageNo":$scope.currentPage,"totalCount":$scope.total};
                 // 2017 /7 /19
                 // 定义一个空对象 , 用于更新和保存数据时临时存储
                 $scope.prod = {};
                 $scope.arrs=response.data;
                 // 定义一个单击删除按钮时触发的事件，用于删除选中行
                 $scope.delete = function($index){
                     if ($index>=0) {
                         if(confirm("是否删除"+$scope.arrs[$index+(($scope.page.pageNo-1)*$scope.page.limit)].subject_name+"科目") ){
                             //将需要删除的卡号传给后台  2017/7/10
                             $http({
                                 method:"POST",
                                 url:"/OA/classsubject/delete",
                                 data:$.param({id:$scope.arrs[$index+(($scope.page.pageNo-1)*$scope.page.limit)].id}),
                                 headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
                             })
                                 .success(function (result) {
                                     $scope.arrs.splice($index,1);
                                     if (result.ok==true) {
                                         window.location.reload();
                                     }
                                 }).error(function(result){
                                 alert(result.error)
                             })
                         }
                     };
                 }

             })
    $scope.change = function(selectedLimit){
        $scope.page.limit = selectedLimit.limit;
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    }
    $scope.pageChanged = function(){
        $scope.page.limit = $scope.selectedLimit.limit;
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    }
    $scope.setPage = function (go) {
        $scope.length = Math.ceil($scope.total/$scope.selectedLimit.limit);
        console.log($scope.length);
        if(go > $scope.length){
            $scope.page.pageNo =  $scope.length;
            console.log($scope.length);

        }else{
            $scope.page.pageNo=go;
        }
        $scope.datas = $scope.data.slice($scope.selectedLimit.limit*($scope.page.pageNo-1),$scope.selectedLimit.limit*$scope.page.pageNo);
    };
    // 添加保存
    $scope.save=function () {
        var class_id=$scope.class_id;
        var subject_id=$("#twosubject").find("option:selected").val();
        var subject_name=$("#twosubject").find("option:selected").text();
        var subject=$("#subjectoption").find("option:selected").val();
        var staff_id=$("#subjectteacher").find("option:selected").val();
        var staff_name=$("#subjectteacher").find("option:selected").text();
        var hour=$("#hourNumber").val();
        $http({
            method:"POST",
            url:"/OA/classsubject/create",
            data:$.param({class_id:class_id,staff_id:staff_id,staff_name:staff_name,subject:subject,subject_id:subject_id,subject_name:subject_name,number:hour}),
            headers: { 'Content-Type': 'application/x-www-form-urlencoded' }
        }).success(function (response) {
            if(response.ok==true){
                $("#studentadd").modal('hide');
                setTimeout(function () {
                    window.location.reload();
                },600)
            }
        })
    }
    // 键盘保存
    $scope.todoSomething=function($event){
        if($event.keyCode==13){//回车
            save();
        }
    }
});
